/*-----------------------------------------------------------------------------------
 *
 * 프로그램명	: HomePageDAO.java
 * Version		: 1.0
 * 작성일 		: 2006-08-16
 * 작성자		:
 * 수정일 		:
 * 수정자		:
 * 설명		 	:
 * 기능		 	:
 *
 * Copyright (c) 2000 by H&H  All Rights Reserved.
 *
-------------------------------------------------------------------------------------*/
package com.welfare.admin.system;

import java.io.*;

import java.util.*;
import java.sql.*;

import com.zll.framework.db.DBManager;
import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;



public class SystemDAO
{
	
	
	public HashList selectPopupInfo(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/system.xml", "select_popup_info", infos);
	}
	
	public HashList selectPopupList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/system.xml", "select_popup_list", infos);
	}
	
	public HashList selectDeptAuthList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/system.xml", "select_dept_auth_list", infos);
	}
	
	public HashList selectUserList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/system.xml", "select_user_list", infos);
	}

	public HashList selectCodeList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/system.xml", "select_code_list", infos);
	}

	public HashList selectSrchCodeList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/system.xml", "select_srch_code_list", infos);
	}
	
	public HashList selectDocumentList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/system.xml", "select_document_list", infos);
	}
	
	public HashList selectDocumentInfo(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/system.xml", "select_document_info", infos);
	}
	
	public String selectDocumentId(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/system.xml", "select_document_id", infos).get("docu_id",0);
	}

	
	/**
	 * 서식 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void saveDocument(HashBox box) throws Exception
    {
		Connection conn = null;

        try
		{
			conn = DBManager.getConnection();

			if ("C".equals(box.get("job_type"))) {
				if (!"".equals(box.get("file_path"))) {
		    		DBManager.executeUpdate("/system.xml", "insert_file", box.getParam(), conn);
				}
	    		DBManager.executeUpdate("/system.xml", "insert_document", box.getParam(), conn);
	       	} else if ("U".equals(box.get("job_type"))) {
	       		if (!"".equals(box.get("file_path"))) {
		       		DBManager.executeUpdate("/system.xml", "delete_document_file", box.getParam(), conn);
		    		DBManager.executeUpdate("/system.xml", "insert_file", box.getParam(), conn);
				}
	       		DBManager.executeUpdate("/system.xml", "update_document", box.getParam(), conn);
	        } else if ("D".equals(box.get("job_type"))) {
	       		DBManager.executeUpdate("/system.xml", "delete_document", box.getParam(), conn);
	       		DBManager.executeUpdate("/system.xml", "delete_file", box.getParam(), conn);
	        }
			

			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }
	
	/**
	 * 팝업 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public String savePopup(HashBox box) throws Exception
    {
		Connection conn = null;
		String popupId = box.get("popup_id");
        try
		{
			conn = DBManager.getConnection();
			
			if ("C".equals(box.get("job_type"))) {
				popupId = DBManager.executeQuery("/system.xml", "select_popup_id", box.getParam()).get("popup_id",0);
				box.put("popup_id", popupId);
	    		DBManager.executeUpdate("/system.xml", "insert_popup", box.getParam(), conn);
	       	} else if ("U".equals(box.get("job_type"))) {
	       		DBManager.executeUpdate("/system.xml", "update_popup", box.getParam(), conn);
	        } else if ("D".equals(box.get("job_type"))) {
	       		DBManager.executeUpdate("/system.xml", "delete_popup", box.getParam(), conn);
	        }
			

			conn.commit();
			
			return popupId;
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }
	
	
	/**
	 * 부서권한 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void saveDeptAuth(HashBox box) throws Exception
    {
		Connection conn = null;
        //boolean result	= true;

        try
		{
			conn = DBManager.getConnection();


			ArrayList dataList = (ArrayList)box.getList("list");

		    for (int i = 0; i < dataList.size(); i++)
		    {
				HashMap map = (HashMap)dataList.get(i);
				if ("U".equals(map.get("job_type"))) {
			    	DBManager.executeUpdate("/system.xml", "delete_dept_auth", map, conn);
			    	
			    	if (!"".equals(map.get("auth_code"))) {
			    		DBManager.executeUpdate("/system.xml", "insert_dept_auth", map, conn);
			    	}
				}
			}


			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }
	
	
	
	/**
	 * User 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void saveUser(HashBox box) throws Exception
    {
		Connection conn = null;

        try
		{
			conn = DBManager.getConnection();

			ArrayList dataList = (ArrayList)box.getList("list");

		    for (int i = 0; i < dataList.size(); i++)
		    {
				HashMap map = (HashMap)dataList.get(i);

				if ("C".equals(map.get("job_type"))) {
		    		DBManager.executeUpdate("/system.xml", "insert_user", map, conn);
		       	} else if ("U".equals(map.get("job_type"))) {
		       		DBManager.executeUpdate("/system.xml", "update_user", map, conn);
		       	} else if ("D".equals(map.get("job_type"))) {
		       		DBManager.executeUpdate("/system.xml", "delete_user", map, conn);
		        }
			}

			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }


	/**
	 * Code 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void saveCode(HashBox box) throws Exception
    {
		Connection conn = null;
        //boolean result	= true;

        try
		{
			conn = DBManager.getConnection();


			//DBManager.executeUpdate("/system.xml", "delete_all_code", box.getParam(), conn);

			ArrayList dataList = (ArrayList)box.getList("list");

		    for (int i = 0; i < dataList.size(); i++)
		    {
				HashMap map = (HashMap)dataList.get(i);
				if ("C".equals(map.get("job_type"))) {
					DBManager.executeUpdate("/system.xml", "insert_code", map, conn);
				} else if ("U".equals(map.get("job_type"))) {
					DBManager.executeUpdate("/system.xml", "update_code", map, conn);
				} else if ("D".equals(map.get("job_type"))) {
					DBManager.executeUpdate("/system.xml", "delete_code", map, conn);
				}
			}


			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }



}
